#pragma once
#include <string>
#include <unordered_map>

namespace ns_fields
{
    #define KEY_METHOD          "method"
    #define KEY_PARAMETERS      "parameters"
    #define KEY_TOPIC_KEY       "topic_key"
    #define KEY_TOPIC_MESSAGE   "topic_message"
    #define KEY_OTYPE           "otype"
    #define KEY_HOST            "host"
    #define KEY_HOST_IP         "ip"
    #define KEY_HOST_PORT       "port"
    #define KEY_RCODE           "rcode"
    #define KEY_RESULT          "result"


    //消息类型定义
    enum class Mtype
    {
        RPC_REQ = 0,
        RPC_RES,
        TOPIC_REQ,
        TOPIC_RES,
        SERVICE_REQ,
        SERVICE_RES
    };

    //响应码类型定义 ————>服务端给客户端响应请求的结果类型
    enum class Rcode
    {
        RCODE_OK = 0,
        RCODE_PARSE_ERROR,
        RCODE_INVALID_MSG,
        RCODE_DISCONNECTTED,
        RCODE_INVALID_PARAMETERS,
        RCODE_NOT_FOUND_SERVICE,
        RCODE_INVALID_OPTYPE,
        RCODE_NOT_FOUND_TOPIC,
        RCODE_INSIDE_ERROR
    };

    std::string errReason(Rcode code)
    {
        static std::unordered_map<Rcode,std::string> errhash = {
            {Rcode::RCODE_OK,"成功"},
            {Rcode::RCODE_PARSE_ERROR,"消息解析失败"},
            {Rcode::RCODE_INVALID_MSG,"无效消息"},
            {Rcode::RCODE_DISCONNECTTED,"连接断开"},
            {Rcode::RCODE_INVALID_PARAMETERS,"无效参数"},
            {Rcode::RCODE_NOT_FOUND_SERVICE,"没有找到对应的服务！"},
            {Rcode::RCODE_INVALID_OPTYPE,"无效的Topic操作类型"},
            {Rcode::RCODE_NOT_FOUND_TOPIC,"找不到主题"},
            {Rcode::RCODE_INSIDE_ERROR,"内部错误"}
        };
        auto it = errhash.find(code);
        if(it == errhash.end())
        {
            return "未知错误";
        }
        return it->second;
    }

    //RPC请求类型
    enum class Rtype
    {
        REQ_SYNC = 0,
        REQ_ASYNC,
        REQ_CALLBACK
    };

    //主题操作类型
    enum class TopicOptype
    {
        TOPIC_CREATE = 0,
        TOPIC_REMOVE,
        TOPIC_SUBSCRIBE,
        TOPIC_CANCEL,
        TOPIC_PUBLISH
    };

    //服务操作类型
    enum class ServiceOptype
    {
        SERVICE_REGISTRY = 0,
        SERVICE_DISCOVER,
        SERVICE_ONLINE,
        SERVICE_OFFLINE,
        SERVICE_UNKNOW
    };


}